/*
 * TagString.cpp
 *
 *  Created on: 18 juin 2011
 *      Author: Pierrick Caillon <pierrick@megami.fr>
 */

#include "TagString.h"
#include "TagShort.h"

/* TAGSTRING_H_ */
namespace NamedBinaryTag
{
TagString::TagString() :
	NamedTag(), value("")
{
}

TagString::~TagString()
{
}

TagTypes TagString::get_TagType() const
{
	return TAG_STRING;
}

void TagString::Read(std::istream & input, bool ignoreName)
{
	NamedTag::Read(input, ignoreName);
	TagShort length;
	length.Read(input, true);
	char *data = new char[length.get_Value() + 1];
	input.read(data, length.get_Value());
	data[length.get_Value()] = 0;
	this->value.assign(data, length.get_Value());
	delete[] data;
}

void TagString::Write(std::ostream & output, bool ignoreName) const
{
	NamedTag::Write(output, ignoreName);
	TagShort length;
	length.set_Value((int16_t) (this->value.length()));
	length.Write(output, true);
	output.write(this->value.data(), this->value.length());
}

std::string TagString::get_Value() const
{
	return this->value;
}

void TagString::set_Value(std::string value)
{
	this->value = value;
}

ITag *TagString::clone() const
{
	return this->clone(NULL);
}

ITag *TagString::clone(void *newRoot) const
{
	TagString *copy = new TagString(newRoot);
	copy->value = this->value;
	return copy;
}

}
